
t_Msg Msg;


static u8 gbTrigKey = 0x00;
static u8 gbContKey = 0x00;
static bool gfHaveLongKey = FALSE;

void KeyScan(u8 *sfr,void *KeyDispose(u8))
{
    bool KeyState;
    bool KeyStateBuf;
    static u8 ShortDebounce;
    static u8 ContKeyBuf;
    static u8 LongDebounce;

    KeyState = 1;

    KeyState = sfr;

    if(KeyState != KeyStateBuf)
    {
        KeyStateBuf = KeyState;
        ShortDebounce = 0;
    }
    else if(ShortDebounce < cShortDebounce)
    {
        bShortDebounce++;
    }
    else
    {
        bShortDebounce = 0;
        bKeyState = bKeyStateBuf^0xff;
        gbTrigKey = bKeyState&(bKeyState^(gbContKey));
        gbContKey = bKeyState;

        if(gbContKey != bContKeyBuf)
        {
                bContKeyBuf = gbContKey;
                bLongDebounce = 0;
        }
        else if(bLongDebounce < cLongDebounce)
        {
                bLongDebounce++
        }
        else
        {
                bLongDebounce = 0;
                fHaveLongKey = TRUE;
        }
    }

    switch(gbTrigKey)
    {
    case Key1:
            (*KeyDispose)(Key1);
            break;        
    case Key2:
            (*KeyDispose)(Key2);
            break;
    case Key3:
            (*KeyDispose)(Key3);
            break;
    default:
            break;
    }
    if(gfHaveLongKey)
    {
            gfHaveLongKey = FALSE;
            switch(gbContKey)
            {
            case Key1:
                    break;        
            case Key2:
                    break;
            case Key3:
                    break;
            default:
                    break;
            }
    }
}




void Drv_UIGetKey1()
{   
    bool key;
    
    key = KEYPIN();
}


void Drv_UIShifter16(u16 dat)
{
    u8 i = 15;
    
    STRPIN(0);
    do
    {
        if(REDBIT(dat,i))
        {
            DATAPIN(1);
        }
        else
        {
            DATAPIN(0); 
        }
        CLKPIN(1);
        CLKPIN(0);  
        
    }while(i--)

    STRPIN(1);
}


void Drv_UI10msTask(t_UIControler *Msg)
{
    u16 temp;
    static u8 FlashCount = 0;
    const u8 Segtab[] =
    {
        #if 0
        /*Common anode*/
        0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
        0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,
        0x00
        #else
        /*Common cathode*/
        0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
        0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,
        0x00
        #endif
    };

    FlashCount++;
    
    if(((Msg.Scan)>>1) == 0x00)
    {
        Msg.Scan = 0x40;
    }
    
    switch(Msg.Scan)
    {
        case 0x40:
            temp = Msg.Led;
            SETBIT(var,bitnum)
            break;
        case 0x20:
            temp = Segtab[Msg.Num1];
            if((FlashCount&FLASHTIME)&&Msg.NumF.Num1F)
            {
                temp = Segtab[DUMMY]; 
            }
            
            break;
        case 0x10:
            temp = Segtab[Msg.Num2];
            if((FlashCount&FLASHTIME)&&Msg.NumF.Num2F)
            {
                temp = Segtab[DUMMY];     
            }
            break;
        case 0x08:
            temp = Segtab[Msg.Num3];
            if((FlashCount&FLASHTIME)&&Msg.NumF.Num3F)
            {
                temp = Segtab[DUMMY];     
            }
            GetKey1();
            GetIn1();
            break;
        case 0x04:
            temp = Segtab[Msg.Num4];
            if((FlashCount&FLASHTIME)&&Msg.NumF.Num4F)
            {
                temp = Segtab[DUMMY];     
            }            
            GetKey2()
            GetIn2();
            break;
        case 0x02:
            temp = Segtab[Msg.Num5];
            if((FlashCount&FLASHTIME)&&Msg.NumF.Num5F)
            {
                temp = Segtab[DUMMY];     
            }            
            GetKey3()
            GetIn3();
            break;
        case 0x01:
            temp = Segtab[Msg.Num6];
            if((FlashCount&FLASHTIME)&&Msg.NumF.Num6F)
            {
                temp = Segtab[DUMMY];     
            }            
            GetKey4()
            GetIn5();
            break;
    }    
    temp = temp<<8;
    temp |= (~Msg->Scan);
    Drv_UIShifter16(temp);
}

